local jianting = fk.CreateSkill {
  name = "th_jianting",
  max_turn_use_time = 1,
}

jianting:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(jianting.name) then
      local numbers = {}
      for _, cid in ipairs(player:getCardIds("h")) do
        local c = Fk:getCardById(cid)
        if c.number > 0 then
          table.insertIfNeed(numbers, c.number)
        end
      end
      table.sort(numbers)
      local ret = {}
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Player.Hand then
              local c = Fk:getCardById(info.cardId)
              if not table.contains(ret, "#th_jianting_least") and (#numbers == 0 or c.number <= numbers[1]) then
                return true
              end
              if not table.contains(ret, "#th_jianting_most") and (#numbers == 0 or c.number >= numbers[#numbers]) then
                return true
              end
            end
          end
        end
      end

      if #ret > 0 then
        event:setSkillData(self, jianting.name, table.connect(event:getSkillData(self, jianting.name) or {}, ret))
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = jianting.name,
      prompt = "#th_jianting-invoke"
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room

    if #room.draw_pile < 4 then
      room:shuffleDrawPile()
      if #room.draw_pile < 4 then
        room:sendLog{
          type = "#NoCardDraw",
          toast = true,
        }
        room:gameOver("")
      end
    end
    local top = room:getNCards(2, "top")
    local bottom = room:getNCards(2, "bottom")
    local all_cards = table.map(table.connect(top, bottom), Util.Id2CardMapper)
    table.sort(all_cards, function(c1, c2)
      return c1.number < c2.number
    end)
    local cards = table.simpleClone(all_cards)
    if cards[#cards - 1].number ~= cards[#cards].number then
      table.remove(cards, #cards)
    end
    if cards[1].number ~= cards[2].number then
      table.remove(cards, 1)
    end

    local poxiParams = { ---@type AskToPoxiParams
      poxi_type = "AskForCardsChosen",
      data = {
        {"Top", top},
        {"Bottom", bottom},
      },
      extra_data = {
        min = 1,
        max = 1,
        skillName = jianting.name,
        prompt = "#th_jianting-view",
        pattern = tostring(Exppattern:new{ id = table.map(cards, Util.IdMapper) })
      },
      cancelable = false
    }

    local ret = room:askToPoxi(player, poxiParams)
    if #ret ~= 0 then
      room:obtainCard(player, ret, nil, fk.ReasonPrey, player, jianting.name)
    end
  end,
}, { check_skill_limit = true })

Fk:loadTranslationTable{
  ["th_jianting"] = "兼听",
  [":th_jianting"] = "每回合限一次，当你失去点数最小或最大的手牌后，你可以观看牌堆顶和牌堆底各两张牌并获得其中一张点数不为唯一最大或唯一最小的牌。",

  ["#th_jianting-invoke"] = "兼听：你可以观看牌堆顶和牌堆底各两张牌并获得其中一张点数不为唯一最大或唯一最小的牌",

  ["#th_jianting-view"] = "兼听：请观看牌堆顶和牌堆底各两张牌，获得其中一张点数不为唯一最大或唯一最小的牌",
}

return jianting
